<?php
namespace App\Http\Controllers\Icon;

use DB;
use Session;
use App\Models\CommentM;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Common\excuteState;
use App\Models\CommentFavourM;
use Illuminate\Database\QueryException;
use App\Models\ReplyM;
/**
* 评论(comment)控制类描述
* @time: 上午12:18:44
* @author: lee
*/
class commentController extends Controller
{
    /**
     * 根据 icon的fid,user的id 
     * 返回 限制条件的记录数
     * @param bigint $iconfid,icon的fid值
     * @param bigint $userid,user的id值
     * @param int $lmt,返回记录限制数
     * @param int $offset,检索偏移量
     */
    static public function loadCmts($iconfid,$userid,$lmt,$offset){
        return excuteState::OKwithDatas(CommentM::loadCmts($iconfid, $userid,$lmt,$offset));
    }
    
    /**
     * 根据 icon的fid,user的id 返回 评论数
     * 返回 限制条件的记录数
     * @param bigint $iconfid,icon的fid值
     * @param bigint $userid,user的id值
     * @param int $lmt,返回记录限制数
     * @param int $offset,检索偏移量
     */
    static  public function loadNewestCmtByPage($iconfid,$userid,$lmt){
        return excuteState::OKwithDatas(CommentM::loadNewestCmtByPage($iconfid, $userid,$lmt));
    }
    
    
    /**
     * 根据 icon的fid,user的id ,comment的fid 限制数目的返回小于该评论fid的记录
     * @param unknown $iconfid
     * @param unknown $userid
     * @param unknown $cmtfid
     * @param number $limit
     */
    static  public  function loadCmtByMinFid($iconfid,$userid,$cmtfid,$limit){
        return excuteState::OKwithDatas(CommentM::loadCmtByMinFid($iconfid,$userid,$cmtfid,$limit));
    }
    
    /**
     * 依据传入的comment信息 ，添加数据记录。
     * @param Request $request，类对象
     * json传入 形式 
     * { "cmtInfo":{"comment":'new comment','userid':userid1,'iconfid':iconfid2} }
     */
    static public function addCmt(Request $request){
        DB::beginTransaction();
        try {
            $instData=json_decode($request->input('cmtInfo'),true);
            // $instData=['comment'=>'ustt say somethings','userid'=>1111,'iconfid'=>76];
            $newID=CommentM::addItem($instData);
            // 保存新的 评论记录的id
            Session::flash('newCmtID',$newID);
            
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::insertError();
        }
    }
    
    /**
     * 依据 comment's id 删除相关项
     * @param bigint $cmtid,comment's id
     */
    static public function deltCmt($cmtid){
        DB::beginTransaction();
        try {
            // 删除comment-reply table
            CommentFavourM::deltCmtFavourByID($cmtid);
            // delete comment-favour table
            ReplyM::deltRpsByCmtID($cmtid);
            CommentM::deltItem($cmtid);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::deleteError();
        }
    }
    
    /**
     * 根据 comment的id和user的id,添加点赞记录
     * @param bigint $cmtid ,comment 的id
     * @param bigint $userid, user's id
     */
    static public function cmtPlusFavour($cmtid,$userid,$becmtuserid){
       DB::beginTransaction();
        try {
            CommentFavourM::addItem($cmtid, $userid);
            commentM::plusFavour($cmtid);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::insertError();
        }
    }
    /**
     * 根据 comment的id和user的id,删除点赞记录
     * @param bigint $cmtid ,comment 的id
     * @param bigint $userid, user's id
     */
    static public function cmtMinFavour($cmtid,$userid){
        DB::beginTransaction();
        try {
            CommentFavourM::deltItem($cmtid, $userid);
            commentM::minFavour($cmtid);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::deleteError();
        }
    }

}

?>